package zucc.lcj.android_finalwork.stars;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.airbnb.lottie.LottieAnimationView;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import zucc.lcj.android_finalwork.FriendsActivity;
import zucc.lcj.android_finalwork.help.HelpActivity;
import zucc.lcj.android_finalwork.LocalImageLoader;
import zucc.lcj.android_finalwork.LoginActivity;
import zucc.lcj.android_finalwork.MyDatabaseHelper;
import zucc.lcj.android_finalwork.R;
import zucc.lcj.android_finalwork.guide.GuideActivity;
import zucc.lcj.android_finalwork.me.MeActivity;

public class StarsActivity extends AppCompatActivity {
    private com.youth.banner.Banner Banner;
    private LocalImageLoader imageLoader;
    private ArrayList<Integer> image;
    private ArrayList<String> imageTitle;
    private TextView p1,p2,p3,p4,p5,p6,p7;
    private MyDatabaseHelper dbHelper;
    private TextView s1,s2,s3,s4,s5,s6,s7;
    private Button l1,l2,l3,l4,l5,l6,l7;
    private String lastTime1,lastTime2,lastTime3,lastTime4,lastTime5,lastTime6,lastTime7;
    private LottieAnimationView lottie1,lottie2,lottie3,lottie4,lottie5,lottie6,lottie7;
    private int islike=1;
    private int sum1,sum2,sum3,sum4,sum5,sum6,sum7;



    @SuppressLint("Range")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stars);
        initDate();
        initView();
        dbHelper = new MyDatabaseHelper(this,"UserStore.db",null,1);


        s1 = (TextView) findViewById(R.id.s1);
        s2 = (TextView) findViewById(R.id.s2);
        s3 = (TextView) findViewById(R.id.s3);
        s4 = (TextView) findViewById(R.id.s4);
        s5 = (TextView) findViewById(R.id.s5);
        s6 = (TextView) findViewById(R.id.s6);
        s7 = (TextView) findViewById(R.id.s7);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor1 = db.rawQuery("select * from Model where model_num=?",new String[]{"1"});
        sum1 = -1;
        cursor1.moveToFirst();
        do{
            sum1++;
        }while(cursor1.moveToNext());
        s1.setText(String.valueOf(sum1));
//        lastTime1 = cursor1.getString(cursor1.getColumnIndex("model_time"));
        cursor1.close();

        Cursor cursor2 = db.rawQuery("select * from Model where model_num=?",new String[]{"2"});
        sum2 = -1;
        cursor2.moveToFirst();
        do{
            sum2++;
        }while(cursor2.moveToNext());
        s2.setText(String.valueOf(sum2));
//        cursor2.moveToFirst();
//        @SuppressLint("Range")
//        int sum2 = cursor2.getInt(cursor2.getColumnIndex("model_sum"));
//        s2.setText(String.valueOf(sum2));
//        lastTime2 = cursor2.getString(cursor2.getColumnIndex("model_time"));
        cursor2.close();

        Cursor cursor3 = db.rawQuery("select * from Model where model_num=?",new String[]{"3"});
        sum3 = -1;
        cursor3.moveToFirst();
        do{
            sum3++;
        }while(cursor3.moveToNext());
        s3.setText(String.valueOf(sum3));
        cursor3.moveToFirst();
//        @SuppressLint("Range")
//        int sum3 = cursor3.getInt(cursor3.getColumnIndex("model_sum"));
//        s3.setText(String.valueOf(sum3));
//        lastTime3 = cursor3.getString(cursor3.getColumnIndex("model_time"));
        cursor3.close();

        Cursor cursor4 = db.rawQuery("select * from Model where model_num=?",new String[]{"4"});
        sum4 = -1;
        cursor4.moveToFirst();
        do{
            sum4++;
        }while(cursor4.moveToNext());
        s4.setText(String.valueOf(sum4));
//        cursor4.moveToFirst();
//        @SuppressLint("Range")
//        int sum4 = cursor4.getInt(cursor4.getColumnIndex("model_sum"));
//        s4.setText(String.valueOf(sum4));
//        lastTime4 = cursor4.getString(cursor4.getColumnIndex("model_time"));
        cursor4.close();

        Cursor cursor5 = db.rawQuery("select * from Model where model_num=?",new String[]{"5"});
        sum5 = -1;
        cursor5.moveToFirst();
        do{
            sum5++;
        }while(cursor5.moveToNext());
        s5.setText(String.valueOf(sum5));
//        cursor5.moveToFirst();
//        @SuppressLint("Range")
//        int sum5 = cursor5.getInt(cursor5.getColumnIndex("model_sum"));
//        s5.setText(String.valueOf(sum5));
//        lastTime5 = cursor5.getString(cursor5.getColumnIndex("model_time"));
        cursor5.close();

        Cursor cursor6 = db.rawQuery("select * from Model where model_num=?",new String[]{"6"});
        sum6 = -1;
        cursor6.moveToFirst();
        do{
            sum6++;
        }while(cursor6.moveToNext());
        s6.setText(String.valueOf(sum6));
//        cursor6.moveToFirst();
//        @SuppressLint("Range")
//        int sum6 = cursor6.getInt(cursor6.getColumnIndex("model_sum"));
//        s6.setText(String.valueOf(sum6));
//        lastTime6 = cursor6.getString(cursor6.getColumnIndex("model_time"));
        cursor6.close();

        Cursor cursor7 = db.rawQuery("select * from Model where model_num=?",new String[]{"7"});
        sum7 = -1;
        cursor7.moveToFirst();
        do{
            sum7++;
        }while(cursor7.moveToNext());
        s7.setText(String.valueOf(sum7));
//        cursor7.moveToFirst();
//        @SuppressLint("Range")
//        int sum7 = cursor7.getInt(cursor7.getColumnIndex("model_sum"));
//        s7.setText(String.valueOf(sum7));
//        lastTime7 = cursor7.getString(cursor7.getColumnIndex("model_time"));
        cursor7.close();

        l1 = (Button) findViewById(R.id.l1);
        l2 = (Button) findViewById(R.id.l2);
        l3 = (Button) findViewById(R.id.l3);
        l4 = (Button) findViewById(R.id.l4);
        l5 = (Button) findViewById(R.id.l5);
        l6 = (Button) findViewById(R.id.l6);
        l7 = (Button) findViewById(R.id.l7);
        lottie1 = (LottieAnimationView) findViewById(R.id.lottie1);
        lottie2 = (LottieAnimationView) findViewById(R.id.lottie2);
        lottie3 = (LottieAnimationView) findViewById(R.id.lottie3);
        lottie4 = (LottieAnimationView) findViewById(R.id.lottie4);
        lottie5 = (LottieAnimationView) findViewById(R.id.lottie5);
        lottie6 = (LottieAnimationView) findViewById(R.id.lottie6);
        lottie7 = (LottieAnimationView) findViewById(R.id.lottie7);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD");
        Date currentDate = new Date(System.currentTimeMillis());
        String currentTime = format.format(currentDate);

        l1.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @Override
            public void onClick(View view) {
                SQLiteDatabase db1 = dbHelper.getWritableDatabase();
                Cursor cursor1 = db1.rawQuery("select * from Model where model_num=? and model_user=? and model_time=?",new String[]{"1",LoginActivity.global_login_user,currentTime});
//                Cursor cursor1 = db1.rawQuery("select * from UserInfo where user_name=?",new String[]{"LoginActivity.global_login_user_id"});
//                if(cursor1.moveToFirst()) {
//                    islike = cursor1.getInt(cursor1.getColumnIndex("islike"));
//                    System.out.println(islike);
//                }
//                cursor1.close();
                if(cursor1.moveToFirst()){
                    Toast.makeText(StarsActivity.this, "不好意思，您今天已经为TA点过赞了", Toast.LENGTH_SHORT).show();
                }else{
                    lottie1.playAnimation();
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("model_time",currentTime);
                    values.put("model_num","1");
                    values.put("model_user",LoginActivity.global_login_user);
                    db.insert("Model", null, values);
                    values.clear();
                    sum1++;
                    s1.setText(String.valueOf(sum1));
//                    String total = String.valueOf(sum1 + 1);
//                    db.execSQL("update Model set model_time=?, model_sum=? where model_num=?",new String[]{currentTime,total,"1"});
//                    Cursor cursor = db.rawQuery("select * from Model where model_num=?",new String[]{"1"});
//                    cursor.moveToFirst();
//                    int sum1 = cursor.getInt(cursor.getColumnIndex("model_sum"));
//                    s1.setText(String.valueOf(sum1));
//                    lastTime1 = currentTime;
//                    cursor.close();
                }
            }
        });
        l2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db1 = dbHelper.getWritableDatabase();
                Cursor cursor1 = db1.rawQuery("select * from Model where model_num=? and model_user=? and model_time=?",new String[]{"2",LoginActivity.global_login_user,currentTime});
//                cursor1.close();
                if(cursor1.moveToFirst()){
                    Toast.makeText(StarsActivity.this, "不好意思，您今天已经为TA点过赞了", Toast.LENGTH_SHORT).show();
                }else{
                    lottie2.playAnimation();
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("model_time",currentTime);
                    values.put("model_num","2");
                    values.put("model_user",LoginActivity.global_login_user);
                    db.insert("Model", null, values);
                    values.clear();
                    sum2++;
                    s2.setText(String.valueOf(sum2));
//                    String total = String.valueOf(sum2 + 1);
//                    db.execSQL("update Model set model_time=?, model_sum=? where model_num=?",new String[]{currentTime,total,"2"});
//                    Cursor cursor = db.rawQuery("select * from Model where model_num=?",new String[]{"2"});
//                    cursor.moveToFirst();
//                    @SuppressLint("Range")
//                    int sum2 = cursor.getInt(cursor.getColumnIndex("model_sum"));
//                    s2.setText(String.valueOf(sum2));
//                    lastTime2 = currentTime;
//                    cursor.close();
                }
            }
        });
        l3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db1 = dbHelper.getWritableDatabase();
                Cursor cursor1 = db1.rawQuery("select * from Model where model_num=? and model_user=? and model_time=?",new String[]{"3",LoginActivity.global_login_user,currentTime});
//                cursor1.close();
                if(cursor1.moveToFirst()){
                    Toast.makeText(StarsActivity.this, "不好意思，您今天已经为TA点过赞了", Toast.LENGTH_SHORT).show();
                }else{
                    lottie3.playAnimation();
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("model_time",currentTime);
                    values.put("model_num","3");
                    values.put("model_user",LoginActivity.global_login_user);
                    db.insert("Model", null, values);
                    values.clear();
                    sum3++;
                    s3.setText(String.valueOf(sum3));
//                    String total = String.valueOf(sum3 + 1);
//                    db.execSQL("update Model set model_time=?, model_sum=? where model_num=?",new String[]{currentTime,total,"3"});
//                    Cursor cursor = db.rawQuery("select * from Model where model_num=?",new String[]{"3"});
//                    cursor.moveToFirst();
//                    @SuppressLint("Range")
//                    int sum3 = cursor.getInt(cursor.getColumnIndex("model_sum"));
//                    s3.setText(String.valueOf(sum3));
//                    lastTime3 = currentTime;
//                    cursor.close();
                }
            }
        });
        l4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db1 = dbHelper.getWritableDatabase();
                Cursor cursor1 = db1.rawQuery("select * from Model where model_num=? and model_user=? and model_time=?",new String[]{"4",LoginActivity.global_login_user,currentTime});
//                cursor1.close();
                if(cursor1.moveToFirst()){
                    Toast.makeText(StarsActivity.this, "不好意思，您今天已经为TA点过赞了", Toast.LENGTH_SHORT).show();
                }else{
                    lottie4.playAnimation();
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("model_time",currentTime);
                    values.put("model_num","4");
                    values.put("model_user",LoginActivity.global_login_user);
                    db.insert("Model", null, values);
                    values.clear();
                    sum4++;
                    s4.setText(String.valueOf(sum4));
//                    String total = String.valueOf(sum4 + 1);
//                    db.execSQL("update Model set model_time=?, model_sum=? where model_num=?",new String[]{currentTime,total,"4"});
//                    Cursor cursor = db.rawQuery("select * from Model where model_num=?",new String[]{"4"});
//                    cursor.moveToFirst();
//                    @SuppressLint("Range")
//                    int sum4 = cursor.getInt(cursor.getColumnIndex("model_sum"));
//                    s4.setText(String.valueOf(sum4));
//                    lastTime4 = currentTime;
//                    cursor.close();
                }
            }
        });
        l5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db1 = dbHelper.getWritableDatabase();
                Cursor cursor1 = db1.rawQuery("select * from Model where model_num=? and model_user=? and model_time=?",new String[]{"5",LoginActivity.global_login_user,currentTime});
//                cursor1.close();
                if(cursor1.moveToFirst()){
                    Toast.makeText(StarsActivity.this, "不好意思，您今天已经为TA点过赞了", Toast.LENGTH_SHORT).show();
                }else{
                    lottie5.playAnimation();
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("model_time",currentTime);
                    values.put("model_num","5");
                    values.put("model_user",LoginActivity.global_login_user);
                    db.insert("Model", null, values);
                    values.clear();
                    sum5++;
                    s5.setText(String.valueOf(sum5));
//                    String total = String.valueOf(sum5 + 1);
//                    db.execSQL("update Model set model_time=?, model_sum=? where model_num=?",new String[]{currentTime,total,"5"});
//                    Cursor cursor = db.rawQuery("select * from Model where model_num=?",new String[]{"5"});
//                    cursor.moveToFirst();
//                    @SuppressLint("Range")
//                    int sum5 = cursor.getInt(cursor.getColumnIndex("model_sum"));
//                    s5.setText(String.valueOf(sum5));
//                    lastTime5 = currentTime;
//                    cursor.close();
                }
            }
        });
        l6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db1 = dbHelper.getWritableDatabase();
                Cursor cursor1 = db1.rawQuery("select * from Model where model_num=? and model_user=? and model_time=?",new String[]{"6",LoginActivity.global_login_user,currentTime});
//                cursor1.close();
                if(cursor1.moveToFirst()){
                    Toast.makeText(StarsActivity.this, "不好意思，您今天已经为TA点过赞了", Toast.LENGTH_SHORT).show();
                }else{
                    lottie6.playAnimation();
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("model_time",currentTime);
                    values.put("model_num","6");
                    values.put("model_user",LoginActivity.global_login_user);
                    db.insert("Model", null, values);
                    values.clear();
                    sum6++;
                    s6.setText(String.valueOf(sum6));
//                    String total = String.valueOf(sum6 + 1);
//                    db.execSQL("update Model set model_time=?, model_sum=? where model_num=?",new String[]{currentTime,total,"6"});
//                    Cursor cursor = db.rawQuery("select * from Model where model_num=?",new String[]{"6"});
//                    cursor.moveToFirst();
//                    @SuppressLint("Range")
//                    int sum6 = cursor.getInt(cursor.getColumnIndex("model_sum"));
//                    s6.setText(String.valueOf(sum6));
//                    lastTime6 = currentTime;
//                    cursor.close();
                }
            }
        });
        l7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db1 = dbHelper.getWritableDatabase();
                Cursor cursor1 = db1.rawQuery("select * from Model where model_num=? and model_user=? and model_time=?",new String[]{"7",LoginActivity.global_login_user,currentTime});
//                cursor1.close();
                if(cursor1.moveToFirst()){
                    Toast.makeText(StarsActivity.this, "不好意思，您今天已经为TA点过赞了", Toast.LENGTH_SHORT).show();
                }else{
                    lottie7.playAnimation();
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("model_time",currentTime);
                    values.put("model_num","7");
                    values.put("model_user",LoginActivity.global_login_user);
                    db.insert("Model", null, values);
                    values.clear();
                    sum7++;
                    s7.setText(String.valueOf(sum7));
//                    String total = String.valueOf(sum7 + 1);
//                    db.execSQL("update Model set model_time=?, model_sum=? where model_num=?",new String[]{currentTime,total,"7"});
//                    Cursor cursor = db.rawQuery("select * from Model where model_num=?",new String[]{"7"});
//                    cursor.moveToFirst();
//                    @SuppressLint("Range")
//                    int sum7 = cursor.getInt(cursor.getColumnIndex("model_sum"));
//                    s7.setText(String.valueOf(sum7));
//                    lastTime7 = currentTime;
//                    cursor.close();
                }
            }
        });

        p1 = (TextView) findViewById(R.id.t1);
        p2 = (TextView) findViewById(R.id.t2);
        p3 = (TextView) findViewById(R.id.t3);
        p4 = (TextView) findViewById(R.id.t4);
        p1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(StarsActivity.this, ZhangshuaiActivity.class);
                startActivity(intent);
            }
        });
        p2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(StarsActivity.this, Chengxueke.class);
                startActivity(intent);
            }
        });
        p3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(StarsActivity.this, Zhoujinhe.class);
                startActivity(intent);
            }
        });
        p4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(StarsActivity.this, Liqingru.class);
                startActivity(intent);
            }
        });

        ImageView bar_1 = (ImageView) findViewById(R.id.bar_1);
        ImageView bar_2 = (ImageView) findViewById(R.id.bar_2);
        ImageView bar_3 = (ImageView) findViewById(R.id.bar_3);
        ImageView bar_4 = (ImageView) findViewById(R.id.bar_4);
        ImageView bar_5 = (ImageView) findViewById(R.id.bar_5);
        bar_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(StarsActivity.this, FriendsActivity.class);
                startActivity(intent);
            }
        });
        bar_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(StarsActivity.this, HelpActivity.class);
                startActivity(intent);
            }
        });
        bar_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(StarsActivity.this, GuideActivity.class);
                startActivity(intent);
            }
        });
        bar_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(StarsActivity.this, MeActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initDate(){
        image = new ArrayList<>();
        imageTitle = new ArrayList<>();
        image.add(R.drawable.p4);
        image.add(R.drawable.p2);
        image.add(R.drawable.p1);
        image.add(R.drawable.p3);
        imageTitle.add("计算机1803-张帅");
        imageTitle.add("金融18级-程雪珂");
        imageTitle.add("统计1802-周津禾");
        imageTitle.add("资产评估1801-李青濡");
    }

    private void initView(){
        imageLoader = new LocalImageLoader();
        Banner = findViewById(R.id.banner);

        Banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        Banner.setImageLoader(imageLoader);
        Banner.setBannerAnimation(Transformer.ZoomOutSlide);
        Banner.setBannerTitles(imageTitle);
        Banner.setDelayTime(4000);
        Banner.isAutoPlay(true);
        Banner.setIndicatorGravity(BannerConfig.CENTER);
        Banner.setImages(image);
        Banner.start();
        Banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(StarsActivity.this, "has clicked"+(position+1)+"zhang", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
